<template>
  <div class="caseManage">
    <el-tabs v-model="activeName" type="card" class="elTabs">
      <el-tab-pane name="first">
        <div slot="label" class="pageHeader">案件管理</div>
      </el-tab-pane>
    </el-tabs>
    <div class="container">
      <!-- 搜索查询 -->
      <div class="searchBar" v-show="showSearch">
        <div class="searchForm">
          <el-form
            :model="queryParams"
            ref="queryForm"
            size="small"
            :inline="true"
            label-width="80px"
          >
            <el-form-item label="案件名称:" prop="caseName">
              <el-select
                v-model.trim="queryParams.caseName"
                filterable
                placeholder="请选择案件名称"
                clearable
                class="input"
              >
                <el-option
                  v-for="item in cases"
                  :key="item.id"
                  :label="item.caseName"
                  :value="item.caseName"
                >
                </el-option>
              </el-select>
              <!-- <el-input
                v-model.trim="queryParams.caseName"
                placeholder="请输入案件名称"
                clearable
                class="input"
                @keyup.enter.native="handleQuery"
              /> -->
            </el-form-item>
            <el-form-item label="案件状态:" prop="caseState">
              <el-select
                class="input"
                v-model="queryParams.caseState"
                placeholder="请选择案件状态"
                clearable
              >
                <el-option
                  v-for="dict in dict.type.case_state"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                /> </el-select
            ></el-form-item>
            <el-form-item label="开始日期~结束日期" label-width="140px">
              <el-date-picker
                class="input"
                v-model="dateRange"
                style="width: 240px"
                value-format="yyyy-MM-dd"
                type="daterange"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
              ></el-date-picker>
            </el-form-item>
          </el-form>
        </div>
        <div class="searchButtons">
          <el-button class="searchButton" @click="handleQuery">搜索</el-button>
          <el-button class="resetButton" @click="resetQuery">重置</el-button>
        </div>
      </div>
      <!-- 新增、批量删除、导入、导出 -->
      <div
        class="addDeleteBar"
        v-if="
          checkPermi(['case:add', 'case:delete', 'case:import', 'case:export'])
        "
      >
        <el-button
          class="addButton"
          v-hasPermi="['case:add']"
          @click.native="handleAdd"
        >
          <span class="plus">+</span>
          新增</el-button
        >
        <el-button
          @click="handleDelete"
          :class="!multiple ? 'deleteButton' : 'allDeleteButton'"
          :disabled="multiple"
          icon="el-icon-delete"
          v-hasPermi="['case:delete']"
          >批量删除</el-button
        >
        <!-- <el-button
          @click.native="handleImport"
          icon="el-icon-download"
          class="deleteButton"
          v-hasPermi="['case:import']"
          >批量导入</el-button
        >
        <el-button
          @click="handleExport"
          class="addButton"
          icon="el-icon-upload2"
          v-hasPermi="['case:export']"
          >导出</el-button
        > -->
      </div>
      <!-- 表单选择清空 -->
      <div class="tableSelectArea">
        <div>
          <div class="iconInfo el-icon-info"></div>
          <span class="infoText">已选择</span>
          <span class="infoText infoActive" v-text="selectedNum"></span>
          <span class="infoText">项</span>
          <span class="infoButton" @click="clearSelect">清空</span>
        </div>
        <right-toolbar
          :showSearch.sync="showSearch"
          @queryTable="getList"
        ></right-toolbar>
      </div>
      <div class="table">
        <el-table
          ref="multipleTable"
          class="tableArea"
          v-loading="loading"
          :data="caseList"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="43" align="center">
          </el-table-column>
          <el-table-column
            type="index"
            label="序号"
            width="70"
            align="center"
            :show-overflow-tooltip="true"
          >
          </el-table-column>
          <el-table-column
            label="案件名称"
            :show-overflow-tooltip="true"
            align="left"
            width="120"
          >
            <template v-slot="scope">
              <div v-text="scope.row.caseName"></div>
            </template>
          </el-table-column>
          <el-table-column
            prop="rulingBookNumber"
            width="130"
            label="判决书号"
            align="left"
            :show-overflow-tooltip="true"
          ></el-table-column>
          <el-table-column
            prop="executeNumber"
            width="130"
            label="执行案号"
            align="left"
            :show-overflow-tooltip="true"
          ></el-table-column>
          <el-table-column
            prop="introduction"
            label="案件简介"
            width="150"
            align="left"
            :show-overflow-tooltip="true"
          ></el-table-column>
          <el-table-column
            prop="qc"
            label="案件所属区域"
            width="200"
            :show-overflow-tooltip="true"
          ></el-table-column>
          <!-- <el-table-column label="案件状态" align="center" width="82">
            <template v-slot="scope"> -->
          <!-- TODO：公共变量抽取 -->
          <!-- dict.type.case_state[0].value通过后端获取字典值 -->
          <!-- <dict-tag
                :class="{
                  statusDone:
                    scope.row.caseState == dict.type.case_state[0].value,
                  statusDoing:
                    scope.row.caseState == dict.type.case_state[1].value,
                }"
                class="dataDoStatus"
                :options="dict.type.case_state"
                :value="scope.row.caseState"
              ></dict-tag>
            </template>
          </el-table-column> -->
          <el-table-column
            prop="involvedPeople"
            label="涉案人数"
            align="center"
            :show-overflow-tooltip="true"
          ></el-table-column>
          <el-table-column
            prop="payedPeople"
            label="已发放人数"
            align="center"
            width="100"
            :show-overflow-tooltip="true"
          ></el-table-column>
          <el-table-column
            prop="involvedMoney"
            label="涉案金额(元)"
            align="center"
            width="130"
            :show-overflow-tooltip="true"
          ></el-table-column>
          <el-table-column
            prop="recoveryLoss"
            label="追回损失(元)"
            align="center"
            width="130"
            :show-overflow-tooltip="true"
          ></el-table-column>
          <el-table-column
            prop="judgmentLossAmount"
            label="判决损失金额(元)"
            align="center"
            width="130"
            :show-overflow-tooltip="true"
          ></el-table-column>
          <el-table-column
            prop="confirmedPayment"
            label="判决认定金额(元)"
            align="center"
            width="130"
            :show-overflow-tooltip="true"
          ></el-table-column>
          <el-table-column
            prop="payedMoney"
            label="已发放金额(元)"
            align="center"
            width="130"
            :show-overflow-tooltip="true"
          ></el-table-column>
          <el-table-column
            prop="payedProportion"
            label="已发放比例(%)"
            width="130"
            align="center"
            :show-overflow-tooltip="true"
          ></el-table-column>
          <el-table-column
            label="操作"
            :width="dynamicWidth"
            fixed="right"
            align="left"
          >
            <template slot-scope="scope">
              <div class="tableButtonArea">
                <el-button
                  type="text"
                  class="operationButton"
                  @click="handleDetail(scope.row)"
                  >详情</el-button
                >
                <el-button
                  type="text"
                  class="operationButton"
                  @click="handleUpdate(scope.row)"
                  v-hasPermi="['case:edit']"
                  >修改</el-button
                >
                <el-button
                  type="text"
                  class="operationButton"
                  @click="handleDelete(scope.row)"
                  v-hasPermi="['case:delete']"
                  >删除</el-button
                >
                <!-- TODO权限判断 -->
                <el-button
                  type="text"
                  class="operationButton"
                  @click="gotoCheckTask(scope.row)"
                  v-hasPermi="['case:task']"
                  >核查任务</el-button
                >
                <el-button
                  type="text"
                  class="operationButton"
                  v-hasPermi="['case:payment']"
                  @click="gotoPayment(scope.row)"
                  >案款发放</el-button
                >
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="tableFooter">
        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </div>
    </div>
    <caseManageModal
      ref="caseManageModal"
      :modalType.sync="modalType"
      :id="id"
      @fatherMethod="fatherMethod"
    ></caseManageModal>
    <caseDetailModal :detailId.sync="detailId"> </caseDetailModal>
    <userUpload ref="userUpload"></userUpload>
  </div>
</template>
<script>
import {
  MODALTYPE,
  CASESTATE,
  PAGESIZE,
  PAGENUM,
  DEPTTYPE,
  ROLETYPE,
  CASEEXPORTURL,
} from "../../utils/variable";
import { fatherMethod } from "../../utils/commonMethods";
import { checkPermi, checkRole } from "@/utils/permission"; // 权限判断函数
import userUpload from "./components/userUpload.vue";
import {
  listCase,
  delCase,
  getRelatedCase,
  getCaseList,
} from "@/api/caseManage/case";
import caseManageModal from "./components/caseManageModal.vue";
import caseDetailModal from "./components/caseDetailModal.vue";
export default {
  dicts: ["case_state"],
  components: { caseManageModal, caseDetailModal, userUpload },
  data() {
    return {
      activeName: "first",
      modalType: "",
      selectedNum: 0,
      id: 0,
      dateRange: [],
      ids: [],
      // 非多个禁用
      multiple: true,
      // 查询参数
      queryParams: {
        pageNum: PAGENUM,
        pageSize: PAGESIZE,
        caseName: "",
        caseState: "",
      },
      //详情页Id
      detailId: 0,
      loading: false,
      // 总条数
      total: 0,
      //  显示搜索条件
      showSearch: true,
      caseList: [],
      cases: [],
    };
  },
  created() {
    this.getList();
    this.getCase();
  },
  computed: {
    dynamicWidth() {
      if (
        this.$store.state.user.dept.deptName == DEPTTYPE[7] ||
        this.$store.state.user.dept.deptName == DEPTTYPE[8]
      ) {
        return "100px";
      } else if (
        this.$store.state.user.dept.deptName == DEPTTYPE[5] ||
        this.$store.state.user.dept.deptName == DEPTTYPE[6] ||
        this.$store.state.user.dept.deptName == DEPTTYPE[2]
      ) {
        return "250px";
      } else {
        return "340px";
      }
    },
  },
  mounted() {},
  provide() {
    return {
      getList: this.getList,
    };
  },
  methods: {
    checkPermi,
    fatherMethod,
    listCase,
    handleImport() {
      // TODO：模态框的打开和关闭是通过直接操作子组件的方式实现的吗？
      this.$refs.userUpload.upload.title = "案件批量导入";
      this.$refs.userUpload.upload.open = true;
    },
    getCase() {
      let params = {
        oucode: this.$store.state.user.oucode,
      };
      getCaseList(params).then((response) => {
        this.cases = response.data;
        console.log(this.cases);
      });
    },
    /** 查询案件列表 */
    getList() {
      this.loading = true;
      this.queryParams.oucode = this.$store.state.user.oucode;
      listCase(this.addDateRange(this.queryParams, this.dateRange)).then(
        (response) => {
          this.caseList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = PAGENUM;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.selectedNum = selection.length;
      this.ids = selection.map((item) => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    //清空列表选择
    clearSelect() {
      this.$refs.multipleTable.clearSelection();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.modalType = MODALTYPE["新增"];
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.modalType = MODALTYPE["修改"];
      this.id = row.id;
    },
    /** 详情按钮操作 */
    handleDetail(row) {
      this.detailId = row.id;
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      let num = this.selectedNum;
      if (row.id) {
        num = 1;
      }
      this.$modal
        .confirm("是否确认删除" + num + "件案件")
        .then(function () {
          return delCase(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    //前往案款发放
    gotoPayment(row) {
      this.$store.commit("SET_CASEID", row.id);
      this.$router.push({
        path: "/paymentManage/paymentManage",
        query: { id: row.id },
      });
    },
    //前往核查任务
    gotoCheckTask(row) {
      this.$store.commit("SET_CASEID", row.id);
      if (this.$store.state.user.dept.deptName == DEPTTYPE[1]) {
        this.$router.push({
          path: "/taskManage/task",
          query: { id: row.id },
        });
      } else {
        this.$router.push({
          path: "/taskManage/waitCompleteTasks",
          query: { id: row.id },
        });
      }
    },
    //上传附件抽屉
    upload(index) {
      this.$refs.UserUpload.upload.title = "上传附件";
      this.$refs.UserUpload.upload.open = true;
    },
    //导出
    handleExport() {
      this.download(
        CASEEXPORTURL,
        {
          ids: this.ids,
        },
        `导出案件_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style lang="scss" scoped>
@import "../../assets/styles/variables.scss";
.caseManage {
  height: 100%;
  .tableFooter {
    text-align: right;
    justify-content: flex-end;
    // margin-top: 25px;
    margin-right: 35px;
    // width: 100%;
    ::v-deep .btn-prev {
      color: #d4d3d3 !important;
      background-color: #fff;
      border: 1px solid #d4d3d3;
    }
    ::v-deep .btn-next {
      color: #d4d3d3 !important;
      background-color: #fff;
      border: 1px solid #d4d3d3;
    }
    ::v-deep .el-pager li {
      background-color: #fff;
      border: 1px solid #d4d3d3;
      font-size: 12px;
      font-family: $font-family;
      font-weight: 400;
      color: #5a5b5d;
    }
  }
  .el-form-item__label {
    font-family: $font-family;
    font-weight: $form-label-font-weight;
    font-size: $form-label-font-size;
    color: $form-label-color;
  }

  .el-input__inner {
    font-size: $form-content-font-size;
    font-family: $font-family;
    font-weight: $form-label-font-weight;
    // color: #AEAFB0;
  }
}
.input {
  width: 150px;
}
::v-deep .el-tabs--card .el-tabs__header {
  background-color: #e4e7ed;
}
::v-deep.el-tabs--card .el-tabs__header .el-tabs__item {
  border-radius: 10px solid black;
  background-color: #fff;
  font-size: 14px;
  font-family: Microsoft YaHei;
  font-weight: 400;
  color: #053ef6;
  line-height: 48px;
  margin-top: 0;
}
</style>
